<!-- Copyright (c) 2022 渝州大数据实验室
 *
 * Lanius is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
-->
<!-- 
* @desc:任务资源搜索组件
* @fileName:ResourcesSearch.vue
* @author:chenxiaofeng@yzbdl.ac.cn
* @date:2022-4-8
!-->
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { Search, Plus } from "@element-plus/icons-vue";
import { useRoute } from "vue-router";
const route = useRoute();

// 接受父组件传回
const props = defineProps({
  // 查询方法
  searchFunc: Function as any,
  // 查询按钮动画
  isSearchLoading: {
    type: Boolean,
    default: false,
  },
  //弹窗显示方法
  dialogControlFunc: Function as any,
});

// 定义名称值
let userValue = ref("");
// 定义资源类型选择值
let eventValue = ref("");
// 定义资源日期范围
let dateValue = ref("");

/**
 * 资源类型下拉框选择项
 */
const options = [
  {
    value: "1",
    label: "作业",
  },
  {
    value: "2",
    label: "转换",
  },
];

/**
 * 查询方法
 * @reture viod
 */
const onSerarch = () => {
  if (dateValue.value === null) {
    dateValue.value = "";
  }
  let searchData = {
    resourceName: userValue.value,
    startTime: dateValue.value[0],
    endTime: dateValue.value[1],
    resourceType: eventValue.value,
  };
  props.searchFunc(searchData);
};

/**
 * 新增方法
 * @reture viod
 */
const onAdd = () => {
  props.dialogControlFunc("新增");
};

// 鼠标是否获取焦点
const isEnters = ref(false);

/**
 * @description: 全局回车事件
 * @return void
 */
const keydownEvent = () => {
  document.onkeydown = (e: any) => {
    if (e.defaultPrevented) {
      return;
    }
    const body = document.getElementsByTagName("body")[0];
    if (
      e.keyCode === 13 &&
      e.target.baseURI.match("/task-schedule/resource") &&
      e.target === body
    ) {
      if (isEnters.value) {
        onSerarch(); //需要调用的方法
      }
    }
  };
};

onMounted(() => {
  let queryVal: any = route.query.resourceType;
  if (queryVal !== undefined) {
    eventValue.value = queryVal;
  }
  keydownEvent();
});
</script>

<template>
  <div class="page-search">
    <!-- 名称查询 -->
    <el-input
      class="page-search-userInput"
      v-model="userValue"
      clearable
      placeholder="请输入名称搜索"
    ></el-input>
    <!-- 时间范围 -->
    <div>
      <el-date-picker
        v-model="dateValue"
        format="YYYY/MM/DD"
        value-format="YYYY-MM-DD HH:mm:ss"
        type="daterange"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
    </div>
    <!-- 资源类型 -->
    <el-select
      class="page-search-eventChoose"
      v-model="eventValue"
      placeholder="请选择资源类型"
      clearable
      size="large"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
    <!-- 查询按钮 -->
    <el-button
      @click="onSerarch"
      @mouseenter="isEnters = true"
      @mouseleave="isEnters = false"
      :loading="props.isSearchLoading"
      :icon="Search"
      >查询</el-button
    >
    <el-button class="add" :icon="Plus" @click="onAdd">新增</el-button>
  </div>
</template>

<style scoped lang="scss">
.page-search {
  display: flex;
  flex-wrap: wrap;
  justify-content: start;
  padding-left: 25px;
  .page-search-userInput,
  .page-search-eventChoose,
  .page-search-time,
  .search,
  .add {
    margin-bottom: 15px;
  }
}
@media screen and (min-width: 1568px) {
  .page-search {
    margin-bottom: 15px;
    display: flex;
    justify-content: end;
  }
}
.page-search {
  :deep(.el-input__wrapper) {
    height: 30px;
    border-radius: 0;
    background-color: #f2f2f6 !important;
    box-shadow: unset !important;
  }
  :deep(.el-date-editor--daterange) {
    border-radius: 0;
    background-color: #f2f2f6 !important;
    box-shadow: unset !important;
  }
  .page-search-userInput,
  .page-search-eventChoose {
    width: 160px;
    margin-left: 10px;
  }
  .el-input {
    margin-right: 10px;
  }
  .el-button {
    margin-left: 10px;
    width: 70px;
    height: 30px;
    border-radius: 0;
    background-color: #f2f2f6;
    color: #000;
    border: 0;
    &:hover {
      background-color: #5473e6;
      color: #fff;
    }
  }
}
</style>
